<?
function OpenDB()
{
    $db  = mysql_connect("server:3306", "username", "password" );
	if( !$db ) echo "Could not connect databse";
    mysql_select_db("database");
	
	$charset = mysql_query("SET NAMES 'utf8'");
	return $db;
}

function CloseDB()
{
	mysql_close();
}

function QueryRecordNo( $table, $condition="1" )
{
	$sql = "SELECT count(*) as TOTAL_COUNT FROM " . $table . " WHERE " . $condition;
	$result = mysql_query( $sql );

	// check result false
	if( $result === FALSE ) return 0;

	// get one row
	$row = mysql_fetch_array( $result );
	mysql_free_result( $result );
	if( $row === FALSE ) return 0;

	// get real record no
	return intval( $row["TOTAL_COUNT"] );
}

function QueryRecordData( $table, $condition="1", $columns_to_get="*" )
{
	$sql = "SELECT " . $columns_to_get . " FROM " . $table . " WHERE " . $condition;
	$result = mysql_query( $sql );

	// prepare var to return
	$result_array = array();

	// check if result is ok
	if( $result === FALSE ) return $result_array;
	
	// loop to fetch all
	while( $row = mysql_fetch_array($result) ) array_push( $result_array, $row );

	return $result_array;
}

















function QueryRemainingAmount( $UserId )
{
	OpenDB();
	
	$sql = "Select sum(Amount) as InputAmt from main where FieldID=9999 and UserID=" . $UserId;	
	$result = mysql_query($sql);
	$row = mysql_fetch_array( $result );
	$InputAmt = $row["InputAmt"];

	if( $InputAmt == 0 || $InputAmt == "" )
	{
		CloseDB();
		return -999999;
	}
	else
	{
		$sql = "Select sum(Amount) as OutputAmt from main where FieldID<>9999 and UserID=" . $UserId;	
		$result = mysql_query($sql);
		$row = mysql_fetch_array( $result );
		$OutputAmt = $row["OutputAmt"];
	}
	
	CloseDB();
	
	return $InputAmt - $OutputAmt;
}

function QueryExactInt( $key, $column, $table )
{
	OpenDB();
	$sql = "Select * from " . $table . " where " . $column . "=" . $key;

	//echo $sql;
	$result = mysql_query($sql);
	
	CloseDB();
	
	return $result;
}

function ModifyField( $id, $name, $description, $bgcolor )
{
	OpenDB();
	
	$sql = "Update fields Set Name='" . $name . "', " .
	       "Description='" . $description . "', " .
		   "BGColor='" . $bgcolor . "' " .
	       "Where AutoID=" . $id . ";";

	if( !mysql_query($sql) )
	{
		echo $sql;
		CloseDB();
		return -1;
	}
	
	CloseDB();
	
	return 0;
}


function InsertField( $name, $description, $bgcolor )
{
	OpenDB();
	
	$sql = "INSERT INTO fields (Name, Description, BGColor) Values ('" .
		   $name . "', '" . $description . "', '" . $bgcolor . "');";	

	//echo $sql;
	if( !mysql_query($sql) )
	{
		echo $sql;
		CloseDB();
		return -1;
	}
	CloseDB();
	
	return 0;
}

function DeleteField( $id )
{
	OpenDB();
	
	$sql = "DELETE from fields where AutoID='" . $id . "';";	

	//echo $sql;
	if( !mysql_query($sql) )
	{
		echo $sql;
		CloseDB();
		return -1;
	}
	CloseDB();
	
	return 0;
}

function GetAllFieldsUTF()
{
	OpenDB();
	$sql = "Select * from fields";

	//echo $sql;
	$charset = mysql_query("SET NAMES 'UTF8'");
	$result = mysql_query($sql);
	
	CloseDB();
	
	return $result;
}
?>
